﻿package com.designoutput.ui
{
    import com.designoutput.enum.ToolType;
    import com.designoutput.event.NumberEvent;
    import com.designoutput.event.SliderEvent;
    import com.designoutput.icon.CurrCords;
    import com.designoutput.icon.CurrSelection;
    import com.designoutput.icon.Transparent;
    import com.designoutput.icon.TransparentLarge;
    import com.designoutput.icon.ZoomMinusOver;
    import com.designoutput.icon.ZoomPlusOver;
    import com.designoutput.ui.newControls.Box;
    import com.designoutput.ui.controls.BoxSplit;
    import com.designoutput.ui.controls.IconButton;
    import com.designoutput.ui.controls.Literal;
    import com.designoutput.ui.controls.NumberInput;
    import com.designoutput.ui.newControls.Slider;
    
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import com.designoutput.manager.ResourceManager;

    public class NavigatorPanel extends Box
    {
		internal var ed:DesignoutputCard;
        internal var overview:Sprite;
        internal var ratio:Number;
        internal var split:BoxSplit;
        internal var id:uint;
        internal var plus:IconButton;
        public var zl:NumberInput;
        internal var nh:int;
        internal var marker:Sprite; // the visual rectangle
        internal var amt:int = 0;
        internal var nw:int;
        internal var bm:Bitmap;
        internal var zoom:Sprite;
        internal var canvas:CardWindow;
        internal var slider:Slider;
        internal var tc:Rectangle;
        internal var minus:IconButton;
		internal var sx:Literal;
		internal var sy:Literal;
        internal var cx:Literal;
        internal var cy:Literal;

        public function NavigatorPanel(parent:DesignoutputCard)
        {
            this.tc = new Rectangle();
            this.ed = parent;
            this.addCloseButton();
            this.setTitle(ResourceManager.getValue("rm_navigator"));
            this.Resize(220, 160);
			
			// the overview
            this.overview = new Sprite();
            this.overview.graphics.beginFill(9474192);
            this.overview.graphics.drawRect(0, 0, 124, 84);
            this.overview.graphics.beginFill(16777215);
            this.overview.graphics.drawRect(1, 1, 122, 82);
            this.overview.graphics.endFill();
            this.overview.x = 10;
            this.overview.y = 25;
            addChild(this.overview);
           
			var currCord:* = new Bitmap(new CurrCords(0, 0));
			currCord.x = 143;
			currCord.y = 37;
            addChild(currCord);
            var liX:Literal = new Literal("X:");
			liX.x = 158;
			liX.y = 28;
            addChild(liX);
            var liY:Literal = new Literal("Y:");
			liY.x = 158;
			liY.y = 40;
            addChild(liY);
            this.cx = new Literal();
            this.cx.autoSize = TextFieldAutoSize.RIGHT;
            this.cx.x = 200;
            this.cx.y = 28;
            addChild(this.cx);
            this.cy = new Literal();
            this.cy.autoSize = TextFieldAutoSize.RIGHT;
            this.cy.x = 200;
            this.cy.y = 40;
            addChild(this.cy);
            var bs:BoxSplit = new BoxSplit(BoxSplit.SMALLTYPE);
			bs.x = 140;
			bs.y = 62;
            addChild(bs);
            var currSelection:* = new Bitmap(new CurrSelection(0, 0));
			currSelection.x = 143;
			currSelection.y = 75;
            addChild(currSelection);
            var liW:* = new Literal("W:");
			liW.x = 155;
			liW.y = 66;
            addChild(liW);
            var liH:* = new Literal("H:");
			liH.x = 158;
			liH.y = 78;
            addChild(liH);
            this.sx = new Literal();
            this.sx.autoSize = TextFieldAutoSize.RIGHT;
            this.sx.x = 197;
            this.sx.y = 66;
            addChild(this.sx);
            this.sy = new Literal();
            this.sy.autoSize = TextFieldAutoSize.RIGHT;
            this.sy.x = 197;
            this.sy.y = 78;
            addChild(this.sy);
           
			this.bm = new Bitmap(null, "auto", true);
            this.bm.x = 2;
            this.bm.y = 2;
            this.overview.addChild(this.bm);
            this.marker = new Sprite();
            this.marker.x = 2;
            this.marker.y = 2;
            this.marker.addEventListener(MouseEvent.MOUSE_DOWN, this.MarkerMouseDown, false, 0, true);
            this.marker.addEventListener(MouseEvent.ROLL_OVER, this.MarkerMouseOver, false, 0, true);
            this.marker.addEventListener(MouseEvent.ROLL_OUT, this.MarkerMouseOut, false, 0, true);
            this.overview.addChild(this.marker);
           
			
			this.zoom = new Sprite();
            this.zoom.x = 10;
            this.zoom.y = 123;
            this.split = new BoxSplit(BoxSplit.LARGETYPE);
            this.split.x = 1;
            this.split.y = 0;
            this.zoom.addChild(this.split);
            this.slider = new Slider(100, 30);
            this.slider.addEventListener(SliderEvent.DRAG, this.ZoomDrag, false, 0, true);
            this.slider.x = 17;
            this.slider.y = 4;
            this.zoom.addChild(this.slider);
            this.zl = new NumberInput(36, 100, 5, 800);
            this.zl.x = 151;
            this.zl.y = 6;
            this.zl.addEventListener(NumberEvent.VALUE_CHANGE, this.SetZoomEvent, false, 0, true);
            this.zoom.addChild(this.zl);
            var li:* = new Literal("%");
			li.x = 188;
			li.y = 6;
            this.zoom.addChild(li);
            this.minus = new IconButton(new ZoomMinusOver(0, 0));
            this.minus.addEventListener(MouseEvent.MOUSE_DOWN, this.ZoomMinus, false, 0, true);
            this.minus.x = -2;
            this.minus.y = 10;
            this.zoom.addChild(this.minus);
            this.plus = new IconButton(new ZoomPlusOver(0, 0));
            this.plus.addEventListener(MouseEvent.MOUSE_DOWN, this.ZoomPlus, false, 0, true);
            this.plus.x = 122;
            this.plus.y = 9;
            this.zoom.addChild(this.plus);
            addChild(this.zoom);
        }

        public function SetSelectionSize(mx:int, my:int) : void
        {
            if (mx != 0 && my != 0)
            {
                this.sx.text = mx.toString();
                this.sy.text = my.toString();
            }
            else
            {
                this.sy.text = "";
                this.sx.text = "";
            }
        }

        public function SetReference(canvas:CardWindow) : void
        {
            this.canvas = canvas;
            if (canvas != null)
            {
                if (canvas.cardRequiredWidth / 120 > canvas.cardRequiredHeight / 90)
                {
                    this.nw = 120;
                    this.nh = Math.round(canvas.cardRequiredHeight * (this.nw / canvas.cardRequiredWidth));
                }
                else
                {
                    this.nh = 90;
                    this.nw = Math.round(canvas.cardRequiredWidth * (this.nh / canvas.cardRequiredHeight));
                }
                this.overview.graphics.clear();
                this.overview.graphics.beginFill(9474192);
                this.overview.graphics.drawRect(0, 0, this.nw + 4, this.nh + 4);
                this.overview.graphics.beginFill(16777215);
                this.overview.graphics.drawRect(1, 1, this.nw + 2, this.nh + 2);
                this.overview.graphics.beginBitmapFill(new Transparent(0, 0));
                this.overview.graphics.drawRect(2, 2, this.nw, this.nh);
                this.overview.graphics.endFill();
                this.ratio = (this.nw + this.nh) / (canvas.cardRequiredWidth + canvas.cardRequiredHeight);
                this.bm.bitmapData = canvas.screenshot;
                this.bm.width = this.nw;
                this.bm.height = this.nh;
                this.RedrawMarker();
                this.SetZoom(canvas.zoomlevel * 100, true, true);
//                if (canvas.selection != null)
//                {
//                    this.SetSelectionSize(param1.selection.width, param1.selection.height);
//                }
//                else
//                {
//                    this.SetSelectionSize(0, 0);
//                }
            }
            else
            {
                this.bm.bitmapData = null;
                this.overview.graphics.clear();
                this.overview.graphics.beginFill(9474192);
                this.overview.graphics.drawRect(0, 0, 124, 84);
                this.overview.graphics.beginFill(16777215);
                this.overview.graphics.drawRect(1, 1, 122, 82);
                this.overview.graphics.endFill();
                this.marker.graphics.clear();
                this.SetSelectionSize(0, 0);
            }
        }

        public function ZoomPlus(event:MouseEvent) : void
        {
            clearInterval(this.id);
            this.id = setInterval(this.AnimateZoomIn, 10);
        }

        public function RedrawMarker() : void
        {
            this.tc.x = Math.round(this.canvas.clip.x * this.ratio);
            this.tc.y = Math.round(this.canvas.clip.y * this.ratio);
            this.tc.width = this.canvas.contentAvailableHeight > this.canvas.masking.width ? (this.nw) : (Math.round(this.canvas.clip.width * this.ratio));
            this.tc.height = this.canvas.contentAvailableHeight > this.canvas.masking.height ? (this.nh) : (Math.round(this.canvas.clip.height * this.ratio));
            this.marker.x = this.tc.x + 2;
            this.marker.y = this.tc.y + 2;
            if (this.tc.width != 0 && this.tc.height != 0)
            {
                if (this.marker.width != this.tc.width || this.marker.height != this.tc.height)
                {
                    this.marker.graphics.clear();
                    this.marker.graphics.beginFill(16777215, 0.25);
                    this.marker.graphics.drawRect(0, 0, this.tc.width, this.tc.height);
                    this.marker.graphics.endFill();
                    this.marker.graphics.lineStyle(1, 16711680, 1, true);
                    this.marker.graphics.moveTo(0, 0);
                    this.marker.graphics.lineTo((this.tc.width - 1), 0);
                    this.marker.graphics.lineTo((this.tc.width - 1), (this.tc.height - 1));
                    this.marker.graphics.lineTo(0, (this.tc.height - 1));
                    this.marker.graphics.lineTo(0, 0);
                }
            }
        }

        public function SetZoom(percentNum:int, flag1:Boolean = false, flag2:Boolean = false) : void
        {
            if (percentNum > 800)
            {
				percentNum = 800;
            }
            else if (percentNum < 5)
            {
				percentNum = 5;
            }
            if (this.canvas != null)
            {
                this.canvas.zoom(percentNum, flag2);
            }
            if (flag1)
            {
                this.zl.text = percentNum.toString();
            }
            if (percentNum <= 100)
            {
				percentNum = (percentNum - 5) / 95 * 30;
            }
            else
            {
				percentNum = 30 + percentNum / 800 * 70;
            }
            this.slider.setP(percentNum / 100);
            this.ed.resetTool();
        }

        private function SetZoomValue() : void
        {
            var percentNum:* = this.GetZoomPercent();
            this.zl.text = Math.round(percentNum).toString();
            if (this.canvas != null)
            {
                this.canvas.zoom(Math.round(percentNum));
            }
            this.ed.resetTool();
        }

        public function ZoomMinus(event:MouseEvent) : void
        {
            clearInterval(this.id);
            this.id = setInterval(this.AnimateZoomOut, 10);
        }

        private function ZoomDrag(evt:SliderEvent) : void
        {
            if (!evt.manual && Math.round(this.GetZoomPercent(this.slider.getValue() * 100)).toString() != this.zl.text)
            {
                this.SetZoomValue();
            }
        }

        public function SetMousePosition(param1:Point) : void
        {
            if (param1 != null)
            {
                this.cx.text = param1.x.toString();
                this.cy.text = param1.y.toString();
            }
            else
            {
                var _loc_2:String = "";
                this.cy.text = "";
                this.cx.text = _loc_2;
            }
        }

        public function GetZoomPercent(param1:int = -1) : Number
        {
            if (param1 == -1)
            {
                param1 = Math.round(this.slider.getValue() * 100);
            }
            if (param1 <= 30)
            {
                return param1 / 30 * 95 + 5;
            }
            return (param1 - 30) * 10 + 100;
        }
		
		private function MarkerMouseDown(event:MouseEvent) : void
		{
//			this.ed.cursor.freeze = true;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, this.MarkerMouseMove, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, this.MarkerMouseUp, false, 0, true);
			this.marker.startDrag(false, new Rectangle(2, 2, this.nw - this.marker.width + 1, this.nh - this.marker.height + 1));
		}

        private function MarkerMouseOver(evt:MouseEvent) : void
        {
            trace("Marker mouse over");
//            this.ed.cursor.Set(ToolType.Hand);
        }

        private function MarkerMouseOut(evt:MouseEvent) : void
        {
            trace("Marker mouse out");
//            this.ed.cursor.Set(ToolType.None);
        }

        private function MarkerMouseUp(evt:MouseEvent) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.MarkerMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.MarkerMouseUp, false);
            this.marker.stopDrag();
//            this.ed.cursor.freeze = false;
            if (evt.target != this.marker)
            {
                trace("Marker mouse up");
//                this.ed.cursor.Set(ToolType.None);
                this.ed.resetTool();
            }
        }

		private function MarkerMouseMove(evt:Event) : void
        {
            if (this.canvas != null)
            {
                this.canvas.SetAnchor(new Point(Math.ceil((this.marker.x - 2) / this.ratio), Math.ceil((this.marker.y - 2) / this.ratio)));
            }
        }

        private function AnimateZoomIn() : void
        {
            this.slider.add(0.01);
            this.SetZoomValue();
            this.ed.resetTool();
            if (this.amt++ == 4)
            {
                clearInterval(this.id);
                this.amt = 0;
            }
        }

        private function AnimateZoomOut() : void
        {
            this.slider.remove(0.01);
            this.SetZoomValue();
            this.ed.resetTool();
            if (this.amt++ == 4)
            {
                clearInterval(this.id);
                this.amt = 0;
            }
        }

        public function SetZoomEvent(evt:NumberEvent) : void
        {
            this.SetZoom(evt.val);
        }

    }
}
